package com.liberation.aidiancan.base;


/**
 * 项目名称： AiDianCan
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 9:13
 * 所有Activity的父类
 */

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.liberation.aidiancan.presenter.BasePresent;
import com.liberation.aidiancan.utils.ActivityUtils;
import com.liberation.aidiancan.view.MvpView;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseActivity<V extends MvpView, T extends BasePresent<V>> extends AppCompatActivity {

	public T presenter;
	private FragmentManager fragmentManager;
	private Fragment showFragment;
	boolean unlock;
	int ishasGesture ; //手势密码是否存在
	//保留所有的activity
	public static List<Activity> activitys=new ArrayList<>();

	//绑定连接
	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		//取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
		window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		//需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		setContentView(setContentViews());
		getSupportActionBar().hide();  //去掉标题栏
		this.presenter = createPresenter();
		Log.e("msg", "onCreate: 创建连接");
		if (presenter != null) {
			this.presenter.attch((V) this);
		}
		//获取布局管理器
		fragmentManager = getSupportFragmentManager();
		//绑定完后，初始化
		init();
		//初始化后，加载数据
		loadData();
	}

	//初始化
	public abstract void init();

	//加载数据
	public abstract void loadData();

	//返回当前布局的id
	public abstract int setContentViews();

	//为创建precenter
	public abstract T createPresenter();

	//取消连接
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (presenter != null) {
			presenter.DeAttch((V) this);
		}
	}

	/**
	 * 管理fragment的显示与隐藏
	 * 参数一:显示fragment的布局id
	 * tag标记:自定义不重复的字符串
	 */
	protected void fragmentManager(int resid, Fragment fragment, String tag) {
		//开启一个事务
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		//隐藏正在显示的Fragment对象
		if (showFragment != null) {
			fragmentTransaction.hide(showFragment);
		}

		Fragment mFragment = fragmentManager.findFragmentByTag(tag);
		if (mFragment != null) {
			fragmentTransaction.show(mFragment);
		} else {
			mFragment = fragment;
			fragmentTransaction.add(resid, mFragment, tag);
		}

		showFragment = mFragment;
		fragmentTransaction.commit();
	}


	public void removeFrament(int resid, Fragment fragment, String tag) {

		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.add(resid, fragment, tag);
		transaction.show(fragment);

		transaction.remove(showFragment);
		showFragment = fragment;
		transaction.commit();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}




	@Override
	protected void onStop() {
		super.onStop();
		if (ActivityUtils.isBackground(this)) {
			unlock = true;
		}
		Logger.e(ActivityUtils.isBackground(this) + " ");
	}


}